package com.wdg.dao.impl;

import com.wdg.dao.UserDao;
import com.wdg.pojo.Reg_user;
import com.wdg.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;

public class UserDaoImpl implements UserDao {
    Connection cn = DBUtil.getCn();

    @Override
    public ResultSet slecteSQL() {
        ResultSet rs = null;

        try {
            Statement st = cn.createStatement();
            String sql = "SELECT * FROM reg_user";
            rs = st.executeQuery(sql);
        } catch (SQLException e) {
        }
        return rs;
    }

    @Override
    public ArrayList<Reg_user> findList() {
        ArrayList<Reg_user> list=new ArrayList<>();
        try {
            Statement st = cn.createStatement();
            String sql = "SELECT * FROM reg_user";
            ResultSet rs = st.executeQuery(sql);

            while (rs.next()){
                Reg_user u=new Reg_user();
                u.setAge(rs.getInt("age"));
                u.setUser(rs.getString("user"));
                u.setPw(rs.getString("pw"));
                u.setId(rs.getInt("id"));
                list.add(u);
            }
        } catch (SQLException e) {
        }
        return list;
    }

    @Override
    public void reg(String user, String pw, Integer age) {
        try {
            Statement st = cn.createStatement();
            String sql = "INSERT INTO reg_user(user,pw,age) VALUES('" + user + "','" + pw + "'," + age + ")";
            st.executeUpdate(sql);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void reg_pst(String user, String pw, Integer age) {
        String sql = "INSERT INTO reg_user(user,pw,age) VALUES(?,?,?)";
        try {
            PreparedStatement pst = cn.prepareStatement(sql);
            pst.setString(1, user);
            pst.setString(2, pw);
            pst.setInt(3, age);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void reg_pst_bean(Reg_user u) {
        String sql = "INSERT INTO reg_user(user,pw,age,grade) VALUES(?,?,?,?)";
        try {
            PreparedStatement pst = cn.prepareStatement(sql);
            pst.setString(1, u.getUser());
            pst.setString(2, u.getPw());
            pst.setInt(3, u.getAge());
            pst.setInt(4, u.getGrade());
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean login(String user, String pw) {
        String sql = "SELECT * FROM reg_user WHERE user=? AND pw=?";
        try {
            PreparedStatement pst = cn.prepareStatement(sql);
            pst.setString(1, user);
            pst.setString(2, pw);
            ResultSet rs = pst.executeQuery();
            if(rs.next()){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public void delUserById(Integer id) {
        String sql="delete from reg_user where id=?";
        try {
            PreparedStatement pst = cn.prepareStatement(sql);
            pst.setInt(1,id);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Reg_user findUserById(Integer id) {
        Reg_user u=null;
        String sql="select * from reg_user where id=?";
        try {
            PreparedStatement pst = cn.prepareStatement(sql);
            pst.setInt(1,id);
            ResultSet rs = pst.executeQuery();
            if(rs.next()){
                u=new Reg_user();
                u.setId(id);
                u.setUser(rs.getString("user"));
                u.setPw(rs.getString("pw"));
                u.setAge(rs.getInt("age"));
            }
            rs.close();
            pst.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return u;
    }

    @Override
    public void modifyUser(Reg_user u) {
        String sql = "UPDATE reg_user SET pw=?,age=? WHERE id=?";
        try {
            PreparedStatement pst = cn.prepareStatement(sql);
            pst.setString(1, u.getPw());
            pst.setInt(2, u.getAge());
            pst.setInt(3,u.getId());
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean checkName(String user) {
        String sql = "SELECT * FROM reg_user WHERE user=?";
        try {
            PreparedStatement pst = cn.prepareStatement(sql);
            pst.setString(1, user);
            ResultSet rs = pst.executeQuery();
            if(rs.next()){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Reg_user getUser(String user) {
        Reg_user u=new Reg_user();
        String sql = "SELECT * FROM reg_user WHERE user=?";
        try {
            PreparedStatement pst = cn.prepareStatement(sql);
            pst.setString(1, user);
            ResultSet rs = pst.executeQuery();
            if(rs.next()){
                u.setId(rs.getInt("id"));
                u.setUser(rs.getString("user"));
                u.setGrade(rs.getInt("grade"));
                u.setPw(rs.getString("pw"));
                u.setAge(rs.getInt("age"));
             }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return u;
    }
}
